package org.example.controller;

import lombok.extern.slf4j.Slf4j;
import org.example.pojo.PageResult;
import org.example.pojo.Result;
import org.example.pojo.Student;
import org.example.pojo.StudentQueryParam;
import org.example.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/students")
public class StudentController {

    @Autowired
    private StudentService studentService;
    /*
    * 条件分页查询
    * */
    @GetMapping
    public Result page(StudentQueryParam studentQueryParam) {
        log.info("分页查询参数: {}, {}, {}, {}, {}", studentQueryParam);
        PageResult<Student> pageResult = studentService.page(studentQueryParam);
        return Result.success(pageResult);
    }

    /*
    * 新增学员
    * */
    @PostMapping
    public Result add(@RequestBody Student student) {
        log.info("新增学员: {}", student);
        studentService.add(student);
        return Result.success();
    }

    /*
    * 根据ID查询学生
    * */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("根据ID查询学生: {}", id);
        Student student = studentService.getById(id);
        return Result.success(student);
    }

    /*
    * 批量删除
    * */
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids) {
        log.info("批量删除: {}", ids);
        studentService.deleteByIds(ids);
        return Result.success();
    }

    /*
    * 违纪处理
    * */
    @PutMapping("/violation/{id}/{score}")
    public Result violation(@PathVariable Integer id, @PathVariable Integer score) {
        log.info("违纪处理: {}, {}", id, score);
        studentService.processViolation(id, score);
        return Result.success();
    }
}
